<?php
class Core_Data_Mappers_Rule extends Core_Data_Mappers_Abstract {
	protected $_businessclass ='Core_Business_Rule';
	protected $_modelclass ='Core_Data_Models_Rule';
	protected $_map=array("Id"=>"id","Name"=>"name");
	
	public function find($id) {
		$row = $this->getDbTable()->fetchAll(array("id = ?"=>$id));
		if (isset($row) && count($row)>0 ) {
			$output = $this->map($row->current());
			return $output;
		} 
		else
			return null;
		
	}
	
	public function add($object) {
		if (!$this->exists($object)) {
			$data = $this->reversemap($object);
			unset($data['id']);
			$id = $this->getDbTable()->insert($data);
			$object->setId($id);
		}
	}
	
	public function edit($object) {
		if ($this->exists($object)) {
			$data = $this->reversemap($object);
			unset($data['id']);
			$id = $this->getDbTable()->update($data, array("id = ?"=>$object->Id));
		}
	}
	
	public function exists($object) {
		if($object instanceof Core_Business_Rule) {
			$o = $this->find($object->Id);
			if (isset($o) && $o instanceof Core_Business_Rule) {
				return true;
			}
		}
		return false;
	}
	
	public function fetchAll($where=null,$count=10,$start=0) {
		$rows = $this->getDbTable()->fetchAll($where,null,$count,$start);
		if (isset($rows) && count($rows)>0) {
			$collection = array();
			foreach ($rows as $item) {
				$collection[] = $this->map($item);
			}
			return $collection;
		}
		return null;	
	}
	
	public static function getById($id) {
		$me = new Core_Data_Mappers_Rule();
		return $me->find($id);
	}
}
?>